﻿# better version of had_sex_with_effect
#
# CHARACTER_1: the character doing the laying with.
# CHARACTER_2: the character getting laid.
# C1_PREGNANCY_CHANCE: the chance for Character 1 to get pregnant.
# C2_PREGNANCY_CHANCE: the chance for Character 2 to get pregnant.
# STRESS_EFFECTS: if yes, enables stress effects
# DRAMA: if yes, enables adultery suspicion and story cycles
#
# sends scope:carn_sex_char_1 and scope:carn_sex_char_2 to carn_on_sex
#
# returns scope:carn_sex_char_1_could_be_impregnated, scope:carn_sex_char_2_could_be_impregnated
# these are yes if the character had a pregnancy chance greater than 0 through this sex effect, and no otherwise
#
# returns scope:carn_sex_char_1_impregnated, scope:carn_sex_char_2_impregnated
# these are yes if the character was impregnated through this sex effect, and no otherwise

carn_had_sex_with_effect = {
	$CHARACTER_1$ = { save_scope_as = carn_sex_char_1 }
	$CHARACTER_2$ = { save_scope_as = carn_sex_char_2 }
	if = {
		limit = {
			has_character_flag = carn_block_next_had_sex_with_effect
		}
		remove_character_flag = carn_block_next_had_sex_with_effect
	}
	else = {
		if = {
			limit = { $C1_PREGNANCY_CHANCE$ > 0 }
			save_scope_value_as = {
				name = carn_sex_char_1_could_be_impregnated
				value = yes
			}
		}
		else = {
			save_scope_value_as = {
				name = carn_sex_char_1_could_be_impregnated
				value = no
			}
		}
		if = {
			limit = { $C2_PREGNANCY_CHANCE$ > 0 }
			save_scope_value_as = {
				name = carn_sex_char_2_could_be_impregnated
				value = yes
			}
		}
		else = {
			save_scope_value_as = {
				name = carn_sex_char_2_could_be_impregnated
				value = no
			}
		}
		if = {
			limit = { this = $CHARACTER_1$ }
			custom_tooltip = carn_had_sex_with_effect.first_person.tt
		}
		else_if = {
			limit = { this = $CHARACTER_2$ }
			custom_tooltip = carn_had_sex_with_effect.other_person.tt
		}
		else = {
			custom_tooltip = carn_had_sex_with_effect.third_person.tt
		}

		if = {
			limit = { $STRESS_EFFECTS$ = yes }
			#Stress
			$CHARACTER_1$ = {
				if = {
					limit = { this = prev }
					stress_impact = {
						lustful = minor_stress_impact_loss
						rakish = minor_stress_impact_loss
					}
				}
				else = {
					hidden_effect = {
						stress_impact = {
							lustful = minor_stress_impact_loss
							rakish = minor_stress_impact_loss
						}
					}
				}	
			}
			$CHARACTER_2$ = {
				if = {
					limit = { this = prev }
					stress_impact = {
						lustful = minor_stress_impact_loss
						rakish = minor_stress_impact_loss
					}
				}
				else = {
					hidden_effect = {
						stress_impact = {
							lustful = minor_stress_impact_loss
							rakish = minor_stress_impact_loss
						}
					}
				}
			}
		}
		

		#TRAIT PREFERENCE ADD ON OVER HERE
		hidden_effect = {
			carn_sex_scene_preference_seed_flag_effect = {
			    char_1 = carn_sex_char_1
			    char_2 = char_sex_char_2
			}
			if = {
				limit = { $STRESS_EFFECTS$ = yes }
				#Stress
				carn_sex_scene_preference_get_flag_effect_both = {
					char_1 = carn_sex_char_1
					char_2 = char_sex_char_2
				}
			}
			after = {
				carn_sex_scene_preference_clear_flags = {
					char_1 = carn_sex_char_1
					char_2 = char_sex_char_2
				}
			}
		}




		#Pregnancy & story cycles
		hidden_effect = {
			if = {
				limit = {
					$CHARACTER_1$ = { is_female = yes }
					$CHARACTER_2$ = { carn_gender_can_impregnate_trigger = yes }
				}
				$CHARACTER_1$ = { 
					if = {
						limit = {
							is_pregnant = no
							age >= define:NChildbirth|MIN_FEMALE_REPRODUCTION_AGE
							age < define:NChildbirth|MAX_FEMALE_REPRODUCTION_AGE
							$CHARACTER_2$ = { age >= define:NChildbirth|MIN_MALE_REPRODUCTION_AGE }
						}
						add_to_variable_list = {
							name = potential_father_variable_list
							target = $CHARACTER_2$
						}
						save_scope_value_as = {
							name = carn_sex_char_1_impregnated
							value = no
						}
						random = {
							chance = $C1_PREGNANCY_CHANCE$
							modifier = {
								factor = carn_birth_control_pregnancy_chance_multiplier
								OR = {
									$CHARACTER_1$ = { has_character_modifier = carn_using_birth_control_modifier }
									$CHARACTER_2$ = { has_character_modifier = carn_using_birth_control_modifier }
								}
							}
							make_pregnant = {
								father = $CHARACTER_2$
							}
							save_scope_value_as = {
								name = carn_sex_char_1_impregnated
								value = yes
							}
						}
						if = {
							limit = {
								$DRAMA$ = yes
								NOT = { has_character_flag = had_story_cycle_peasant_affair }
								is_ruler = yes
								$CHARACTER_2$ = {
									is_lowborn = yes
									is_landed = no
									is_courtier = no
								}
							}
							set_variable = {
								name = story_peasant_affair_target
								value = $CHARACTER_2$
							}
							create_story = story_peasant_affair
						}
					}
				}
			}
			else_if = {
				limit = {
					$CHARACTER_1$ = { carn_gender_can_impregnate_trigger = yes }
					$CHARACTER_2$ = { is_female = yes }
				}
				$CHARACTER_1$ = {
					if = {
						limit = {
							NOT = { $CHARACTER_2$ = { has_trait = pregnant } }
						}
						$CHARACTER_2$ = {
							add_to_variable_list = {
								name = potential_father_variable_list
								target = $CHARACTER_1$
							}
						}
						if = {
							limit = {
								$DRAMA$ = yes
								NOT = { has_character_flag = had_story_cycle_peasant_affair }
								is_ruler = yes
								$CHARACTER_2$ = {
									is_lowborn = yes
									is_landed = no
									is_courtier = no
								}
							}
							set_variable = {
								name = story_peasant_affair_target
								value = $CHARACTER_2$
							}
							$CHARACTER_2$ = {
								if = {
									limit = {
										is_pregnant = no
										age < define:NChildbirth|MAX_FEMALE_REPRODUCTION_AGE
									}
									random = {
										chance = $C2_PREGNANCY_CHANCE$
										modifier = {
											factor = carn_birth_control_pregnancy_chance_multiplier
											OR = {
												$CHARACTER_1$ = { has_character_modifier = carn_using_birth_control_modifier }
												$CHARACTER_2$ = { has_character_modifier = carn_using_birth_control_modifier }
											}
										}
										add_character_flag = {
											flag = peasant_is_pregnant
											days = 560
										}
									}
								}
							}
							create_story = story_peasant_affair
						}
						else_if = {
							limit = {
								age >= define:NChildbirth|MIN_MALE_REPRODUCTION_AGE
								$CHARACTER_2$ = {
									is_pregnant = no
									age >= define:NChildbirth|MIN_FEMALE_REPRODUCTION_AGE
									age < define:NChildbirth|MAX_FEMALE_REPRODUCTION_AGE
								}
							}
							save_scope_value_as = {
								name = carn_sex_char_2_impregnated
								value = no
							}
							$CHARACTER_2$ = {
								random = {
									chance = $C2_PREGNANCY_CHANCE$
									modifier = {
										factor = carn_birth_control_pregnancy_chance_multiplier
										OR = {
											$CHARACTER_1$ = { has_character_modifier = carn_using_birth_control_modifier }
											$CHARACTER_2$ = { has_character_modifier = carn_using_birth_control_modifier }
										}
									}
									make_pregnant = {
										father = $CHARACTER_1$
									}
									save_scope_value_as = {
										name = carn_sex_char_2_impregnated
										value = yes
									}
								}
							}
						}
					}
				}
			}
			
			#STD transmission
			hidden_effect = {
				$CHARACTER_1$ = { risk_of_std_from_effect = { PARTNER = $CHARACTER_2$ } }
				$CHARACTER_2$ = { risk_of_std_from_effect = { PARTNER = $CHARACTER_1$ } }
			}
		
			#Adultery suspicion
			hidden_effect = {
				$CHARACTER_1$ = {
					if = {
						limit = {
							$DRAMA$ = yes
							OR = {
								is_married = yes
								is_concubine = yes
							}
							NOT = { any_consort = { this = $CHARACTER_2$ } }
						}
						set_variable = {
							name = had_recent_sex_with
							value = $CHARACTER_2$
							days = 1000
						}
						random = {
							chance = 50 #This should be quite high as there is a chance that adultery.0001 "fails" i.e. spouse doesn't realize
							every_relation = {
								type = soulmate
								limit = {
									NOT = { this = $CHARACTER_2$ } #Soulmates should not grow suspicous of adulterous affairs after sleeping with their lover
								}
								add_to_temporary_list = partners_1
							}
							every_consort = {
								add_to_temporary_list = partners_1
							}
							random_in_list = {
								list = partners_1
								limit = { is_ai = no }
								alternative_limit = { always = yes }
								trigger_event = {
									id = adultery.0001
									days = { 14 365 }
								}
							}
						}
					}
				}
				$CHARACTER_2$ = {
					if = {
						limit = {
							$DRAMA$ = yes
							OR = {
								is_married = yes
								is_concubine = yes
							}
							NOT = { any_consort = { this = $CHARACTER_1$ }}
						}
						set_variable = {
							name = had_recent_sex_with
							value = $CHARACTER_1$
							days = 1000
						}
						random = {
							chance = 50 #This should be quite high as there is a chance that adultery.0001 "fails" i.e. spouse doesn't realize
							every_relation = {
								type = soulmate
								limit = {
									NOT = { this = $CHARACTER_1$ } #Soulmates should not grow suspicous of adulterous affairs after sleeping with their lover
								}
								add_to_temporary_list = partners_2
							}
							every_consort = {
								add_to_temporary_list = partners_2
							}
							random_in_list = {
								list = partners_2
								limit = { is_ai = no }
								alternative_limit = { always = yes }
								trigger_event = {
									id = adultery.0001
									days = { 14 365 }
								}
							}
						}
					}
				}
			}

			if = {
				limit = { $DRAMA$ = yes }
				
				#Incestuous Relations
				$CHARACTER_1$ = {
					give_incest_secret_or_nothing_with_target_effect = { CHARACTER = $CHARACTER_2$ }
				}
				hidden_effect = {
					$CHARACTER_2$ = { give_incest_secret_or_nothing_with_target_effect = { CHARACTER = $CHARACTER_1$ } }
				}

				#Homosexuality
				$CHARACTER_1$ = {
					give_homosexual_secret_or_nothing_with_target_effect = { CHARACTER = $CHARACTER_2$ }
				}
				hidden_effect = {
					$CHARACTER_2$ = { give_homosexual_secret_or_nothing_with_target_effect = { CHARACTER = $CHARACTER_1$ } }
				}
			}
		}

		# hook into new on_action
		$CHARACTER_1$ = {
			$CHARACTER_2$ = { save_scope_as = carn_sex_partner }
			trigger_event = {
				on_action = carn_on_sex
			}
		}
		$CHARACTER_2$ = {
			$CHARACTER_1$ = { save_scope_as = carn_sex_partner }
			trigger_event = {
				on_action = carn_on_sex
			}
		}
	}
}